<?php 
$budgeterr=$ugSerr=$msSerr=$phdSerr=$ugHerr=$msHerr=$phdHerr=$yearerr="";
$budget=$ugsaa=$mssaa=$phdsaa=$ugh=$msh=$phdh=$year="";
session_start();
include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
$post=0;
if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$bud=$_POST['semBudget'];
		$semType=$_POST['semesterType'];
		if (empty($bud))
    	{
			$budgeterr = "Budget is required";
		}
		else
    	{   // $sem = clean_input($semType);
    		$budget = clean_input($bud);
				if (!preg_match("/^[0-9]*$/",$budget))
			   {
					  $budgeterr = "Invalid bugdet format";
			   }
		}
		$year=$_POST["semYear"];
		if (empty($year))
    	{
			$yearerr = "Year is required";
		}
		else
    	{   // $sem = clean_input($semType);
    		$year = clean_input($year);
				if (!preg_match("/^[0-9]{4}$/",$year))
			   {
					  $yearerr = "Invalid Year format";
			   }
		}
		
		if (empty($_POST["ugSAA"]))
		{$ugSerr = "Is required";}
		else
		{
			$ugsaa = clean_input($_POST["ugSAA"]);
			if (!preg_match("/^[0-9]*$/",$ugsaa))
			{
			  		$ugSerr = "Invalid Format";
			}
		}
		if (empty($_POST["msSAA"]))
		{$msSerr = "Is required";}
		else
		{
			$mssaa = clean_input($_POST["msSAA"]);
			if (!preg_match("/^[0-9]*$/",$mssaa))
			{
			  		$msSerr = "Invalid Format";
			}
		}
		if (empty($_POST["phdSAA"]))
		{$phdSerr = "Is required";}
		else
		{
			$phdsaa = clean_input($_POST["phdSAA"]);
			if (!preg_match("/^[0-9]*$/",$phdsaa))
			{
			  		$phdSerr = "Invalid Format";
			}
		}
		if (empty($_POST["ugHOUR"]))
		{$ugHerr = "Is required";}
		else
		{
			$ugh = clean_input($_POST["ugHOUR"]);
			if (!preg_match("/^[0-9]*$/",$ugh))
			{
			  		$ugHerr = "Invalid Format";
			}
		}
		if (empty($_POST["msHOUR"]))
		{$msHerr = "Is required";}
		else
		{
			$msh = clean_input($_POST["msHOUR"]);
			if (!preg_match("/^[0-9]*$/",$msh))
			{
			  		$msHerr = "Invalid Format";
			}
		}
		if (empty($_POST["phdHOUR"]))
		{$phdHerr = "Is required";}
		else
		{
			$phdh = clean_input($_POST["phdHOUR"]);
			if (!preg_match("/^[0-9]*$/",$phdh))
			{
			  		$phdHerr = "Invalid Format";
			}
		}
		
		
	if($budgeterr=="" and $ugSerr=="" and $msSerr=="" and $phdSerr=="" and $ugHerr=="" and $msHerr=="" and $phdHerr=="")
{
					$post=1;
		//after all validations are passed
		$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
		if(!$con)
		{
			die("Error: ".mysql_error());
			}
		mysql_select_db($_DB_NAME,$con);
		//Replace with original variables
		
		mysql_query("SET @pterm='".$_POST["semesterType"]."'");
			mysql_query("SET @pyear=".$_POST["semYear"]);
			mysql_query("SET @pbudget=".$_POST["semBudget"]);		
			mysql_query("SET @pugSAA=".$_POST["ugSAA"]);
			mysql_query("SET @pmsSAA=".$_POST["msSAA"]);
			mysql_query("SET @phdSAA=".$_POST["phdSAA"]);
			mysql_query("SET @pugHour=".$_POST["ugHOUR"]);
			mysql_query("SET @pmsHour=".$_POST["msHOUR"]);
			mysql_query("SET @pphdHour=".$_POST["phdHOUR"]);
		if (!mysql_query("call  Createsemester(@pterm,@pyear,@pbudget,@pugSAA,@pmsSAA,@phdSAA,@pugHour,@pmsHour,@pphdHour)",$con))
		  {
		  die('Error:'+ mysql_error());
		  }
			  
}
	
}
				
				
				
	
function clean_input($data)
{
	  $data = trim($data);
	  $data = stripslashes($data);
	  $data = htmlspecialchars($data);
	  return $data;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Semster</title>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />

<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  
	 <script type="text/javascript">
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		</script>
        
</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

              <ul>
              	<li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>          

</div>
</center>
<span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
        <div class="footbody">
<form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" autocomplete="off">
        <table >
        <thead>
         <tr >
             <td rowspan="2" colspan="2">
             <label class="Header1">Create Semester</label>
             </td>
             </tr>
             </thead>
             <tbody>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
         	  <tr>
              <td />
                  <td>
                  	<label class="crimson-text" >Select a semester </label>
                  </td>
                  <td>
             
                        <select name="semesterType" class="crimson-text"  id="term" onchange="populateYear()">
                    
                     <option value="Fall"<?php
					  if (isset($semType) && $semType=="Fall") echo "selected='selected'";
					  else echo "";?>>Fall</option>
                   <option value="Spring"<?php
					  if (isset($semType) && $semType=="Spring") echo "selected='selected'";
					  else echo "";?>>Spring</option>

                      </select>
                  </td>
                  </tr>
 
            <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
          
                  
            <tr>
            	
              <td />
                
                <td>
                    <label class="crimson-text">Provide a year </label>
                </td>
                <td>
                    <input id="year" class="crimson-border" name="semYear" type="text" onkeyup="populateYear()" value="<?= $year?>"/>
                </td>
                <td class="error">
       <?php echo $yearerr;?>
            </tr>
            
			<tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
          
            
            <tr>
            
            	<td />
                
                <td>
                    <label class="crimson-text">Budget </label>
                 </td>
                 <td>
                    <input type="text" class="crimson-border" name="semBudget" value="<?php echo $budget;?>"/> 
                </td>
 <td class="error">
       <?php echo $budgeterr;?>
                </td>
            </tr>
            
            <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
   			</tbody>
            </table>
             <br />
           <table class="costchart" rowspan="2" colspan="5">
                <thead>
                <tr> 
                <td/>
                <td colspan="2">
                Cost details for the new semester
                </td>
                </tr>
                </thead>
                <tbody>
                <tr>
                	<td> </td>
                    <td> BS</td>
                    <td></td>
                    <td>MS</td>
                     <td></td>
                    <td>PHD</td>
                     <td></td>
                </tr>
                    <tr >
                    <td> SAA </td>
                    <td><input type="text" name="ugSAA" value="<?php echo $ugsaa;?>"/></td><td class="error" >
     <?php echo $ugSerr;?>
        </td>
                    
                    <td><input type="text" name="msSAA" value="<?php echo $mssaa;?>"/></td><td class="error" ><?php echo $msSerr;?>
        </td>
                    <td><input type="text" name="phdSAA" value="<?php echo $phdsaa;?>"/></td><td class="error">
     <?php echo $phdSerr;?>
        </td>
                    </tr>
                    <tr>
                    <td> Hourly </td>
                    <td><input type="text" name="ugHOUR" value="<?php echo $ugh;?>"/></td>
                    <td class="error">
     <?php echo $ugHerr;?>
        </td>
                    <td><input type="text" name="msHOUR" value="<?php echo $msh;?>"/></td>
                    <td class="error">
     <?php echo $msHerr;?>
        </td>
                    <td><input type="text" name="phdHOUR" value="<?php echo $phdh;?>"/></td>
                    <td class="error">
     <?php echo $phdHerr;?>
        </td>
                    </tr>
                 </tbody>
                </table>
                </td>
                </tr>
                <tr/>
                <tr/>
            <tr/>
			<tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            <tr/>
            </tbody>
            </table>
          
                      <br /><br/><br />
               	 	<input type="submit"  value="Create"></input>

          </form>
        </div>
</body>
</html>